package com.xiaoyushu.framework.biz.context.interceptor;

import com.xiaoyushu.framework.biz.context.holder.LoginUserContextHolder;

import com.xiaoyushu.framework.common.constant.GlobalConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;



import java.util.Objects;



public class FeignRequestInterceptor implements RequestInterceptor {


    /**
     * @param requestTemplate
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 获取当前上下文中的用户 ID
        Long userId = LoginUserContextHolder.getUserId();
        // 若不为空，则添加到请求头中
        if (Objects.nonNull(userId)) {
            requestTemplate.header(GlobalConstants.USER_ID, String.valueOf(userId));
            System.out.println("########## feign 请求设置请求头 userId: {}"+ userId);
        }
    }
}

